/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oimwrapper.testers;

import Thor.API.tcResultSet;
import com.thortech.util.logging.Logger;
import com.thortech.xl.dataaccess.tcSqlTypes;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.oimwrapper.connectors.OIMRemoteClientBase;

/**
 *
 */
public class ClientBaseTester {

    private static final Logger logger = Logger.getLogger(ClientBaseTester.class.getName());
    
    private static SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    public OIMRemoteClientBase connect() throws Exception
    {
        logger.debug("Starting ");
        OIMRemoteClientBase tester = null;
        logger.debug("Logging In ");
        try {
            tester = new OIMRemoteClientBase();
            System.setProperty("XL.HomeDir", TesterConfig.XL_HomeDir);
            tester.setConnectionInfo(TesterConfig.XL_USERID);
            tester.connect();
            return tester;
        } catch (Exception ex) {
            logger.error("Exception",ex);
            throw new Exception("Exception",ex);
        }
    }
    
    public void printResultSet(tcResultSet rs)
    {
        try
        {
            String[] colNames = rs.getColumnNames();
            int rowCount = rs.getRowCount();
            for(int i=0;i<rowCount;i++)
            {
                rs.goToRow(i);
                logger.debug("Printing row:" + i);
                for(int c=0;c<colNames.length;c++)
                {
                    if (rs.getColumnType(colNames[c]) == tcSqlTypes.DATE)
                    {
                        Date date = rs.getDate(colNames[c]);
                        logger.debug(colNames[c] + ":" + df.format(date));
                    }
                    else
                    {
                        logger.debug(colNames[c] + ":" + rs.getStringValue(colNames[c]));
                    }
                }
            }
            
        }
        catch(Exception e)
        {
            logger.error("API Error",e);
        }
    }
    
    public void printResultSet(tcResultSet rs, Map filter)
    {
        try
        {
            String[] colNames = rs.getColumnNames();
            int rowCount = rs.getRowCount();
            for(int i=0;i<rowCount;i++)
            {
                rs.goToRow(i);
                logger.debug("Printing row:" + i);
                for(int c=0;c<colNames.length;c++)
                {
                    if (!filter.containsKey(colNames[c]))
                        continue;
                    
                    if (rs.getColumnType(colNames[c]) == tcSqlTypes.DATE)
                    {
                        Date date = rs.getDate(colNames[c]);
                        logger.debug(colNames[c] + ":" + df.format(date));
                    }
                    else
                    {
                        logger.debug(colNames[c] + ":" + rs.getStringValue(colNames[c]));
                    }
                }
            }
            
        }
        catch(Exception e)
        {
            logger.error("API Error",e);
        }
    }
    
    
}
